package br.com.srv.componentes.login.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;

import br.com.srv.commons.dao.hibernate.BasicDAOImpl;
import br.com.srv.model.ClienteTO;

@SuppressWarnings("unchecked")
public class LoginDAOImpl extends BasicDAOImpl implements LoginDAO {

	public ClienteTO findClienteByLogin(String login) {
		Criteria criteria = getSession().createCriteria(ClienteTO.class);
		criteria.add(Expression.eq("login", login));
		
		return (ClienteTO)criteria.uniqueResult();
	}

	public List<ClienteTO> buscarClientes(ClienteTO clienteTO) throws Exception {
		
		int count = 0;
		String query = "SELECT cliente FROM ClienteTO cliente";
		
		if (clienteTO.getLogin() != null && !clienteTO.getLogin().equals("")) {
			query = montarQuery(query, count);
			query += "cliente.login like '" + clienteTO.getLogin() + "%'";
			count++;
		}
		
		if (clienteTO.getDescricao() != null && !clienteTO.getDescricao().equals("")) {
			query = montarQuery(query, count);
			query += "cliente.descricao like '" + clienteTO.getDescricao() + "%'";
			count++;
		}
		
		query += " order by cliente.login";
		
		return findByQuery(query);
	}	

}
